/*
 * Use it like this:
 * <form class="need_validation">
		<input class="required"/>
		<input class="required" data="{validation:{minlength:5}}" />
		<input data="{validation:{required:true,minlength:5}}" />
		<input class="required digits" />
		<input class="number" />
		<input class="required email" />
	</form>
*/
$(function(){
    (function() {
        var js_element = document.createElement('script');
        js_element.type = 'text/javascript';
        js_element.src = "/media/js/jquery.validate.pack.js";
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(js_element);
        $(js_element).load(function(){
            $.metadata.setType('attr','data');
            $('form.need_validation').each(function(i,obj){
                $(obj).before('<div id="validation_form_msg_' + i + '" class="remind remind_error" style="display:none;"></div>').validate({
                    meta:'validation',
                    invalidHandler: function(e, validator) {
                        var errors = validator.numberOfInvalids();
                        if (errors) {
                            var message = '有 <strong style="color:red">'  + errors + (errors == 1
                                ? '</strong> 项为必填,请正确填写！'
                                : '</strong> 项没有按要求填写！ ') + ' ';
                            $("#validation_form_msg_" + i).html(message);
                            $("#validation_form_msg_" + i).show();
                        } else {
                            $("#validation_form_msg" + i).hide();
                        }
                    },
                    
                    
                    
                    messages: {
                        chr_typename: "请输入分类名称！",
                        chr_title:"请输入新闻名称！",
                        chr_source:"请输入正确的链接！",
                        chr_ticks:"请输入数字!",
                        chr_advtypename:"请输入广告分类名称！",
                        chr_advname:"请输入广告名称！",
                        chr_url:"请输入正确的链接！",
                        chr_name:"请输入参数名!",
                        chr_fieldname:"请输入字段名！",
                        chr_value:"请输入相应的值!",
                        chr_flinkname:"请输入网站名!",
                        chr_order:"请输入数字！",
                        chr_email:"请输入有效的邮箱地址!",
                        title:"请输入标题",
                        name:"请输入正确的名称",
                        price:"请输入价格",
                        price_cont:"收费说明",
                        weatherurl:"请输入正确的链接！",
                        address:"请输入正确的地址",
                        name_index:"首字母必填",
                        views:"请输入数字！",
                        been:"请输入数字！",
                        want:"请输入数字！",
                        name_en:"英文名必填!",
                        name_url:"地址栏链接必填!",
                        mapcode:"地址链接有误!"
                    }
//                    submitHandler: function(form) {
//                        jQuery(form).ajaxSubmit({
//                            target: "#result"
//                        });
//                    }
                
                });
            });
        });
    })(); 
});
